#!/bin/bash - 
#===============================================================================
#
#          FILE: makePro.sh
# 
#         USAGE: ./makePro.sh 
# 
#   DESCRIPTION: 根据用户输入的目录和项目名称创建项目
# 
#       OPTIONS: [-p] [目录] [-n] [项目名称]
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: HuiDong (C), HDong_work@126.com
#  ORGANIZATION: Personal
#       CREATED: 2013年10月17日 13:40
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error


ScriptVersion="1.0"

#===  FUNCTION  ================================================================
#         NAME:  usage
#  DESCRIPTION:  Display usage information.
#===============================================================================
usage ()
{
	cat <<- EOT

  欢迎使用 ${0##/*/}     版本：1.0
  Copyright (C)  2013-2014 HuiDong
  做树要有根，为人要有心，做好自己！
  Bug report to HDong_work@126.com

  用法:  ${0##/*/} [options] [参数] 
  选项: 
         -h|help       Display this message
         -v|version    Display script version
         -p|path       Directory of project
         -n|name       The project's name

  注意： 使用时请确保以root权限运行!

	EOT
}    # ----------  end of function usage  ----------

#-----------------------------------------------------------------------
#  Handle command line arguments
#-----------------------------------------------------------------------

flag=0

while getopts ":hvp:n:" opt
do
  case $opt in

    h|help     )  usage; exit 0   ;;

    v|version  )  echo "$0 -- Version $ScriptVersion"; exit 0   ;;

	p|path     ) 
		flag=$(($flag+1))
		pPath=$OPTARG
		if [ ! -d  $pPath ]; then
			echo "The path $pPath does not exist!"
			exit 0
		fi ;;

    n|name	   )  pName=$OPTARG; flag=$(($flag+1)) ;;

    \? )  echo -e "\n  Option does not exist : $OPTARG\n"
          usage; exit 1   ;;

  esac    # --- end of case ---
done
shift $(($OPTIND-1))

if [ ! $flag -ne 0 ]; then
	usage;
	exit 0
fi

if [ $flag -ne 2 ]; then
	echo ""
	echo "  Options are wrong! Please use option -h to get help"
	echo ""
	exit 0
fi

echo `perl /usr/src/project/project.pl $pPath $pName`

which autoscan > /dev/null 2>&1
if [ $? -eq 0 ]; then
	cd $pPath/$pName && autoscan
	mv $pPath/$pName/configure.scan $pPath/$pName/configure.in
else
	note;
fi

if [ -f /usr/share/automake-1.11/depcomp ]; then
	cp /usr/share/automake-1.11/depcomp $pPath/$pName
fi

if [ -f /usr/share/automake-1.11/compile ]; then
	cp /usr/share/automake-1.11/compile $pPath/$pName
fi

note ()
{
	cat <<- EOT

  利用AutoTools组织项目：
  所必须的软件：autoconf/automake/m4/perl/libtool
  (1) 进入到目录：$pPath/$pName 下
  (2) 运行autoscan命令
  (3) 将configure.scan文件重命名为configure.in,并修改configure.in文件
  (4) 在相应的目录下编写Makefile.am文件
  (5) 将/usr/share/automake-1.X/目录下的depcomp和complie文件拷贝到本目录下
  (6) 运行aclocal命令
  (7) 运行autoconf命令
  (8) 运行automake --add-missing命令
  (9) 运行./configure脚本

	EOT
}    # ----------  end of function usage  ----------
